<script setup lang="ts">
import { ref } from "vue";
import ReCol from "@/components/ReCol";
import { formRules } from "./utils/rule";
import { FormProps } from "./utils/types";
import { ElMessage } from "element-plus";
import { QNURL } from "@/config/baseURL";
import { imageUpload } from "@/api/user";
import { Plus } from "@element-plus/icons-vue";
import type { UploadProps } from "element-plus";
// import { usePublicHooks } from "./hooks";
// import { baseUrl } from "@/utils/http";

const props = withDefaults(defineProps<FormProps>(), {
  formInline: () => ({
    id: 0,
    img: "",
    introduce: "",
    title: ""
  })
});

const ruleFormRef = ref();
// const { switchStyle } = usePublicHooks();
const newFormInline = ref(props.formInline);

const imageUrl = ref("");
// const pathUrl = ref("");

const handleAvatarSuccess: UploadProps["onSuccess"] = (
  response,
  uploadFile
) => {
  imageUrl.value = URL.createObjectURL(uploadFile.raw!);
  console.log(imageUrl.value);
};

const beforeAvatarUpload: UploadProps["beforeUpload"] = file => {
  const isJpgOrPng = file.type === "image/jpeg" || file.type === "image/png";
  const isLt2M = file.size / 1024 / 1024 < 2;
  if (!isJpgOrPng) {
    ElMessage.error("只能上传 JPG/PNG 图片格式的文件");
    return false;
  }
  if (!isLt2M) {
    ElMessage.error("文件大小超过 2MB 的限制");
    return false;
  }

  // 使用自定义的方式上传文件
  const formData = new FormData();
  formData.append("image", file);

  uploadFile(formData);

  return false;
};

const uploadFile = async (formData: FormData) => {
  console.log(formData);
  // 自定义上传方法
  // 在这里可以使用你喜欢的方式发送文件上传请求，例如使用axios或者fetch
  // 替换为你自己的上传地址
  // const uploadUrl = "/upload";
  // 发送请求上传文件
  // 例如使用axios：
  try {
    const res = await imageUpload(formData);
    if (res.code === 200) {
      imageUrl.value = QNURL + res?.msg;
      newFormInline.value.img = res?.msg;
    }
  } catch (error) {
    console.error(error);
  }
};

function getRef() {
  return ruleFormRef.value;
}

defineExpose({ getRef });
</script>

<template>
  <el-form
    ref="ruleFormRef"
    :model="newFormInline"
    :rules="formRules"
    label-width="100px"
  >
    <re-col>
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="newFormInline.title"
          clearable
          placeholder="请输入标题"
        />
      </el-form-item>
    </re-col>
    <re-col>
      <el-form-item label="介绍" prop="introduce">
        <el-input
          v-model="newFormInline.introduce"
          clearable
          placeholder="请输入介绍"
        />
      </el-form-item>
    </re-col>

    <el-row :gutter="30">
      <re-col>
        <el-form-item label="封面:" prop="img">
          <el-upload
            class="avatar-uploader"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img
              v-if="newFormInline.img"
              :src="QNURL + newFormInline.img"
              class="img"
            />
            <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
          </el-upload>
        </el-form-item>
      </re-col>
    </el-row>
  </el-form>
</template>

<style scoped>
.avatar-uploader .el-upload {
  position: relative;
  overflow: hidden;
  cursor: pointer;
  border-radius: 6px;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  width: 200px;
  height: 120px;
  font-size: 28px;
  color: #8c939d;
  text-align: center;
  border: 1px dashed var(--el-border-color);
}
</style>
